<?php
declare (strict_types=1);

namespace addons\V2\admin\controller;

use addons\Builder\facade\Builder;
use addons\V2\admin\model\SpecItem;
use think\facade\View;
use think\response\Json;

class Spec extends \app\admin\controller\Base
{
    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\addons\V2\admin\model\Spec';

    //验证器
    protected $validate = 'Spec';

    //关联模型
    protected $hasOne = ['cate1'];


    public function index()
    {


        return Builder::Table()
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'key', 'title' => '规格名'])
            ->addColumn(['field' => 'value', 'title' => '规格值'])
            ->addColumn(['field'   => 'cate', 'title' => '所属专区',
                         'templet' => '<div>{{d.cate1.title}}</div>'])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120, 'unresize' => true])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200])
            ->addTopButtons(['add' => ['size' => '50%'], 'deleteAll'])
            ->addRightButtons(['edit' => ['size' => '50%'], 'delete'])
            ->addSwitchTool([['name' => 'status', 'pk' => 'id', 'text' => '开启|关闭']])
            ->addSearch([
                ['key__value', '输入规格名/规格值', 'input,Like'],
                ['cate_1', '所属专区', 'select', \addons\V2\admin\model\ProductCate::column('title', 'id')]
            ])
            ->fetch();
    }


    public function add($tpl = '')
    {
        return parent::add($this->app->getRootPath() . 'addons/V2/admin/view/spec/edit.html'); // TODO: Change the autogenerated stub
    }

    public function edit($id = '', $tpl = '', $info = '')
    {
        View::assign('item', SpecItem::where('spec_id', $id)->select());
        return parent::edit($id, $this->app->getRootPath() . 'addons/V2/admin/view/spec/edit.html', $info); // TODO: Change the autogenerated stub
    }


    public function save($param = [], $model = true): Json
    {
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }

    public function getSpec()
    {
        $param = $this->request->get();
        return json($this->model::getSpec($param['id'], 'cate_1', $param['pid']));
    }

}
